package com.crscd.ai.controller;

import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 10/11/25 2:22 PM
 * @modifiedBy:
 * @version: 1.0
 */
@RestController
public class BailianRagController {
  @Autowired private ChatClient chatClient;
  @Autowired private DashScopeApi dashScopeApi;

  @GetMapping("/bailian/rag/chat")
  public Flux<String> chat(@RequestParam(name = "msg", defaultValue = "如何编制调车计划") String msg) {
    // 百炼平台RAG构建器
    DocumentRetriever retriever =
        new DashScopeDocumentRetriever(
            dashScopeApi,
            DashScopeDocumentRetrieverOptions.builder().withIndexName("综调系统操作").build());

    return chatClient.prompt().user(msg).advisors(new DocumentRetrievalAdvisor(retriever)).stream()
        .content();
  }
}
